import type { SogouElements } from '../../../../../../../../types/constants/personalization/sogou'
import { ThemeVarService } from '../../../../../../../../lib/abstract/style/theme_var/service'
import { PageHandler } from '../../../../../../../../lib/page_handler'
import { SogouPageType } from '../../../../../../../../lib/page_handler/platform/sogou/types'
import {
  AppStyleCollections,
  generateHideSelector,
  PAGE_ELEMENTS,
  SUPPORTED_PLATFORMS,
} from '../../../../../../../../types/constants'
import { ThemeType } from '../../../../../../../../types/interfaces'
import { PageBaseStyles } from '../../../base'
import { SearchCommonStyles } from '../../common'

export class SogouCommonPage {
  elements: SogouElements = PAGE_ELEMENTS[SUPPORTED_PLATFORMS.SO_GOU]
  private static instance: SogouCommonPage
  private readonly themeVarService: ThemeVarService = ThemeVarService.getInstance()
  private readonly pageBaseStyles: PageBaseStyles = PageBaseStyles.getInstance()
  private readonly searchCommonStyles: SearchCommonStyles = SearchCommonStyles.getInstance()
  private readonly pageType = PageHandler.getCurrentPageType()

  /**
   * 获取单例实例
   */
  public static getInstance(): SogouCommonPage {
    if (!SogouCommonPage.instance) {
      SogouCommonPage.instance = new SogouCommonPage()
    }
    return SogouCommonPage.instance
  }

  init(): void {
  }

  baseWebResultStyles(theme: ThemeType): string {
    return `
/* 顶部菜单 */
#sogou_wrap_id {
  /* 隐藏样式 */
  .header:has(${generateHideSelector('.searchnav')}) {
    padding-bottom: 16px !important;
    
    & + #wrapper #main .search-info {
      margin-top: 0 !important;
    }
  }

  .header {
    background-color: var(${this.themeVarService.search.header.background}) !important;
    border-bottom: none !important;
    padding-bottom: 32px !important;
    
    &.headsearch {
      .header-box {
        flex-direction: column !important;
      }
    }
        
    .header-box {
      display: flex !important;
      justify-content: center !important;
      align-items: center !important;
      
      .logo {
        top: 0 !important;
        
        .logo-normal {
          display: ${theme === ThemeType.DARK ? 'none' : 'inline'} !important;
        }
        
        .logo-white {
          display: ${theme === ThemeType.DARK ? 'inline' : 'none'} !important;
        }
      }
      
      .top-bar {
        > li {
          > a {
            color: var(${this.themeVarService.search.default.text}) !important;
            
            &:hover {
              color: var(${this.themeVarService.link.default.text}) !important;
            }
          }
        }
      }
      
      #searchForm {
        #querybox_up {
          margin-left: 0 !important;
        }
      }
    }
  }
  
  #wrapper {
    padding: 0 !important;

    #main {
      width: 100%;
      min-width: 100% !important;
      max-width: 100% !important;
      padding-right: 0 !important;
      
      /* 搜索标签 */
      .top-hintBox {
        margin: 0 auto !important;
        margin-top: 16px !important;
        width: fit-content !important;
        max-width: 50% !important;
        
        [data-tag="uphintTag"] {
          left: -40px !important;
          
          &::before {
            background: transparent !important;
          }
        }
      }
      
      /* 搜索描述 */
      .search-info {
        display: flex !important;
        justify-content: center !important;
        align-items: center !important;
        gap: 10px !important;
        top: 0 !important;
        margin-bottom: 10px !important;
        margin-top: 16px !important;
        
        .num-tips {
          order: 1 !important;
        }
        
        .all-time-box {
          order: 2 !important;
          position: relative !important;
          top: 0 !important;
          
          .all-time {
            color: var(${this.themeVarService.link.default.text}) !important;
            
            &:hover {
              color: var(${this.themeVarService.link.default.textHover}) !important;
              text-decoration: none !important;
            }
          }
        }
      }
      
      .filter_box {
        margin: 0 auto !important;
        margin-bottom: 16px !important;
      }

      /* 搜索结果列表 */
      .results {
        ${this.searchCommonStyles.resultContainerStyles()}
        
        /* 非结果项 */
        /* 大家还在搜 */
        .middle-better-hintBox {
          .vrTitle {
            color: var(${this.themeVarService.search.default.text}) !important;
          }
        }
        
        /* 推荐搜索 */
        .click-better-sugg {
          border-top: none !important;

          span {
            color: var(${this.themeVarService.search.default.text}) !important;
          }
          
          i {
            display: none !important;
          }
        }

        /* 结果项 */
        > :is(.vrwrap, .reactResult, .rb) {
          ${this.searchCommonStyles.resultItemStyles({ relativePosition: false, noMarginBottom: true })}
          
          /* 标题 */
          h3, div[class*=titleWrap], .vrTitle {
            ${this.searchCommonStyles.titleStyles()}
          }
          
          /* 去掉标题的下划线 */
          a:has(> [class*="title__titleContainer"]) {
            [class*="title__titleText"] {
              color: var(${this.themeVarService.link.default.text}) !important;
            }

            &:hover {
              text-decoration: none !important;
              
              [class*="title__titleText"] {
                color: var(${this.themeVarService.link.default.textHover}) !important;
              }
              
              em {
                text-decoration: none !important;
              }
            }
          }
          
          span[class*=movie-struct__titleWrap] {
            text-decoration: none !important;
          }
          
          .sa-color-text-secondary, [class*=one-result__text] {
            color: var(${this.themeVarService.search.default.text}) !important;
          }
        }
        
        > .rb {
          margin-bottom: 0 !important;
        }
        
        /* AI 回答 */
        .${AppStyleCollections.Search.class.SEARCH_AI_ANSWER} {
          [class*=aiFrame] {
            border: none !important;
            margin-bottom: 0 !important;
            padding: 0 !important;

            .analysis-steps__titleAiSearchIcon_bcda {
              /* AI 名称 */
              [class^=icon__logoText] {
                color: var(${this.themeVarService.search.default.text}) !important;
              }
            }
            
            .analysis-steps__titleTxt_bcda {
              color: var(${this.themeVarService.search.default.text}) !important;
            }
            
            [class^=analysis-content__contentBlock] {
              color: var(${this.themeVarService.search.default.text}) !important;
              
              p {
                color: var(${this.themeVarService.search.default.text}) !important;
              }
              
              ul li::before {
                color: var(${this.themeVarService.search.default.text}) !important;
              }
              
              ol li::before {
                color: var(${this.themeVarService.search.default.text}) !important;
              }
            }
          }
        }
      }
    }
  }
  
  /* 相关搜索 */
  .hintBox {
    display: flex !important;
    justify-content: center !important;
    padding: 0 !important;
  }
}
`
  }

  webAdResultStyles(): string {
    const supportPages = [
      SogouPageType.Web,
      SogouPageType.Ask,
      SogouPageType.Encyclopedia,
    ]

    return !supportPages.includes(this.pageType as SogouPageType)
      ? ''
      : `
/* 广告结果项 */
#sogou_wrap_id #wrapper #main .sponsored-ams {
  display: grid !important;
  grid-template-columns: repeat(1, auto);
  justify-content: center !important;
  gap: ${this.themeVarService.search.resultItem.gap} !important;
  width: 100%;

  > .ad_result {
    background: var(${this.themeVarService.search.resultItem.background}) !important;
    box-shadow: var(${this.themeVarService.search.resultItem.shadow}) !important;
    border-radius: ${this.themeVarService.search.resultItem.borderRadius} !important;
    padding: ${this.themeVarService.search.resultItem.padding} !important;
    overflow: hidden !important;
  
    &:last-child {
      margin-bottom: ${this.themeVarService.search.resultItem.gap} !important;
    }
    
    .pc-style-rb {
      background: transparent !important;
    }
    .r-sech {
      border-top: none !important;
    }
  
    a, .p-item-text {
      color: var(${this.themeVarService.link.default.text}) !important;
    }
    
    div:has(> h4) {
      width: 100% !important;
    }

    /* 标题*/
    h4 {
      ${this.searchCommonStyles.titleStyles('span')}
    }
    
    .p-longsublink {
      .p-lsb-item {
        border-bottom: none !important;
        
        .p-lsb-tag {
          color: var(${this.themeVarService.search.default.text}) !important;
        }
      }
    }
    
    .p-img-wrapper, .p-img-wraper {
      border: none !important;
    }
  }
}
`
  }

  pageBarStyles(): string {
    const supportPages = [
      SogouPageType.Web,
      SogouPageType.Weixin,
      SogouPageType.Zhihu,
      SogouPageType.Video,
      SogouPageType.Medical,
      SogouPageType.Ask,
      SogouPageType.Encyclopedia,
      SogouPageType.News,
      SogouPageType.Knowledge,
    ]

    const defaultSelectorConfig = {
      container: '#pagebar_container',
      currentPage: 'span',
      otherPage: 'a',
      padding: '13px 0 0 0',
    }

    const selectorConfig: Partial<Record<string, { container: string, currentPage: string, otherPage: string, padding: string }>> = {
      [SogouPageType.Web]: defaultSelectorConfig,
      [SogouPageType.Weixin]: defaultSelectorConfig,
      [SogouPageType.Zhihu]: defaultSelectorConfig,
      [SogouPageType.Video]: {
        container: '.play_list_pg',
        currentPage: 'a.on',
        otherPage: 'a:not(.on)',
        padding: '0',
      },
      [SogouPageType.Medical]: defaultSelectorConfig,
      [SogouPageType.Ask]: defaultSelectorConfig,
      [SogouPageType.Encyclopedia]: defaultSelectorConfig,
      [SogouPageType.News]: defaultSelectorConfig,
      [SogouPageType.Knowledge]: defaultSelectorConfig,
    }
    const currentSelectorConfig = selectorConfig[this.pageType as SogouPageType]

    return !supportPages.includes(this.pageType as SogouPageType) || !currentSelectorConfig
      ? ''
      : `
/* 搜索分页 */
${this.pageBaseStyles.pageBarStyles({
  container: currentSelectorConfig.container,
  currentPage: currentSelectorConfig.currentPage,
  otherPage: currentSelectorConfig.otherPage,
})}

${currentSelectorConfig.container} {
  width: 100% !important;
  display: flex;
  justify-content: center !important;
  padding: ${currentSelectorConfig.padding} !important;
  }
}
`
  }

  emStyles(): string {
    const supportPages = [
      SogouPageType.Web,
      SogouPageType.Weixin,
      SogouPageType.Zhihu,
      SogouPageType.Medical,
      SogouPageType.Ask,
      SogouPageType.Encyclopedia,
      SogouPageType.News,
      SogouPageType.Knowledge,
      SogouPageType.Video,
    ]

    return !supportPages.includes(this.pageType as SogouPageType)
      ? ''
      : `
      em {
        color: var(${this.themeVarService.em.text}) !important;
      }
      `
  }

  footerStyles(): string {
    const supportPages = [
      SogouPageType.Web,
      SogouPageType.Weixin,
      SogouPageType.WeixinNotFound,
      SogouPageType.Zhihu,
      SogouPageType.Medical,
      SogouPageType.Ask,
      SogouPageType.Encyclopedia,
      SogouPageType.News,
      SogouPageType.Knowledge,
    ]

    return !supportPages.includes(this.pageType as SogouPageType)
      ? ''
      : `
/* 页脚 */
#s_footer {
  background-color: var(${this.themeVarService.search.default.background}) !important;
  text-align: center !important;
  padding: 0 !important;
  
  a {
    color: var(${this.themeVarService.link.default.text}) !important;
    
    &:hover {
      color: var(${this.themeVarService.link.default.textHover}) !important;
      text-decoration: none !important;
    }
  }
}
`
  }

  getMainStyles(): string {
    return `
/*  搜索分类 */
ul.searchnav, ul.header-nav {
  padding: 0 !important;
  
  /* 更多 */
  li.navMore, li.header-nav-more {
    font-family: arial, sans-serif !important;
    
    > a {
      color: var(${this.themeVarService.search.default.text}) !important;
      font-family: arial, sans-serif !important;
      
      &:hover {
        color: var(${this.themeVarService.link.default.text}) !important;
        text-decoration: none !important;
      }
    }
  }

  li:not(.navMore, .header-nav-more) {
    /* 悬浮 */
    &:hover {
      a {
        color: var(${this.themeVarService.tab.textHover}) !important;
        text-decoration: none !important;
      }
    }
    
    /* 当前页 */
    &.cur {
      border-bottom-color: var(${this.themeVarService.tab.textActive}) !important;

      a {
        color: var(${this.themeVarService.tab.textActive}) !important;
      }
    }
    
    /* 其他页 */
    a {
      color: var(${this.themeVarService.tab.text}) !important;
      font-family: arial, sans-serif !important;
    }
  }
}

/* 右侧边栏 */
#wrapper {
  position: relative !important;
  
  #right {
    position: absolute !important;
    right: 100px;
    top: 48px;
    z-index: 10 !important;
    
    /* 还在问 */
    [class*=titleContainer] {
      [class*=pageNumber] {
        span {
          color: var(${this.themeVarService.search.default.text}) !important;
          
          &[class*=active] {
            color: var(${this.themeVarService.pageBar.currentPage.text}) !important;
          }
        }
      }
    }
    
    /* 广告 */
    #ad_result_right {
      .pc-style-rb {
        background-color: transparent !important;
      }
    }
    
    /* 全网热搜榜 */
    .hot-title-pager {
      .page-number {
        color: var(${this.themeVarService.search.default.text}) !important;
        
        &.active {
          color: var(${this.themeVarService.pageBar.currentPage.text}) !important;
        }
      }
    }
    
    /* 新闻热点*/
    #news_words {
      #news_words_ul {
        border-top: none !important;
        
        li {
          border-bottom: none !important;
        }
      }
    }
  }
}

${this.webAdResultStyles()}
${this.emStyles()}
${this.pageBarStyles()}
${this.footerStyles()}
`
  }
}
